/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function makeObjRequest(){
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

function parsecommand(id,Stringcommand){
    var stringcommand = document.getElementById(Stringcommand).value;
    var loginuser = document.getElementById('loginuser').innerHTML ;
    if(stringcommand.toString().charAt(0)=='/'){
        var req = makeObjRequest();
        var newstrcmd = stringcommand.toString().replace("/"," ")
        var command = newstrcmd.toString().split(" ")
        //execute(parse);
        //document.getElementById("feedbackchatbox").innerHTML = parse.length;
        //document.getElementById("feedbackchatbox").innerHTML = ":"+parse[0]+":"+parse[1]+":"+parse[2]+":";
        if (command.length > 1 && command.length < 5) {
            if (command.length == 2) {
                if (command[1] == "info") {
                    setFeedbackcommand("showing info...");
                    processingcommand(command[1], "", "");
                } else if (command[1]  == "exit" ) {
                    setFeedbackcommand("exit...");
                    processingcommand(command[1], "", "");
                } else {
                    setFeedbackcommand("Wrong command!");
                }
            } else if (command.length == 3) {
                if (command[1] == "create") {
                    setFeedbackcommand("creating #" + command[2] + "...");
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "join") {
                    setFeedbackcommand(command[1] + "ing #" + command[2] + "...");
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "chat") {
                    setFeedbackcommand("creating " + command[1] + " room with " + command[2] + "...");
                    chatroom(loginuser.toString(), command[2])
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "addfriend") {
                    setFeedbackcommand("add " + command[2] + " as friend...");
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "info") {
                    setFeedbackcommand("showing " + command[2] + "'s " + command[1] + "rmation");
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "kick") {
                    setFeedbackcommand(command[1]+" "+command[2]+" from this room...");
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "ban") {
                    setFeedbackcommand(command[1]+" "+command[2]+" from this room...");
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "unban") {
                    setFeedbackcommand(command[1]+" "+command[2]+" from this room...");
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "persist") {
                    setFeedbackcommand("set "+command[1]+" "+command[2]);
                    processingcommand(command[1], command[2], "");
                } else if (command[1] == "setowner") {
                    setFeedbackcommand("change owner to "+command[2]);
                    processingcommand(command[1], command[2], "");
                } else {
                    setFeedbackcommand("Wrong command!");
                }

            } else if (command[1] == "create") {
                //fungsi create room dg nama command[1]
                setFeedbackcommand("creating #" + command[2] + " in " + command[3] + "...");
                processingcommand(command[1], command[2], command[3]);
            }
        } else {
            setFeedbackcommand("Wrong command!");
        }
    }else{
        //setFeedback(id,"masuk ke kota chat gan....");
        chatmessage(id,stringcommand,Stringcommand);
    }
}

function setFeedbackcommand(str){
    document.getElementById("feedbackchatbox").innerHTML = str;
}

function setFeedback(id,feedback){
    var parent = document.getElementById(id);
    var child = document.createElement('div');
    child.innerHTML = feedback;
    parent.appendChild(child);
}

function processingcommand(cmd1,cmd2,cmd3){
    var inputchatbox = document.getElementById("inputchatboxtext");
    inputchatbox.value = "";
    var req = makeObjRequest();
    req.onreadystatechange= function(){
        if (req.readyState==4 && req.status==200)
        {
            setTimeout(function(){
                document.getElementById("feedbackchatbox").innerHTML= req.responseText;
            }, 2324);
        }
    }
    req.open("GET","ProcessingCommand?value1="+cmd1+"&value2="+cmd2+"&value3="+cmd3);
    req.send();
}

function chatmessage(id, strmsg, idinput){
    var inputchatbox = document.getElementById("inputchatboxtext");
    inputchatbox.value = "";
    var req = makeObjRequest();
    req.onreadystatechange= function(){
        if (req.readyState==4 && req.status==200)
        {
            document.getElementById(id).innerHTML= req.responseText;
        }
    }
    req.open("GET","ProcessingChatMessage?value2="+strmsg+"&value3="+idinput);
    req.send();
}
